<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/8/3
 * Time: 15:06
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Recycle extends Base
{
    /**
     * 产品首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $recycle_brand_id = input('recycle_brand_id');
            $recycle_models_id = input('recycle_models_id');
            $recycle_name = trim(input('recycle_name'));
            $recycle_status = input('recycle_status');
            
            $where['recycle_status'] = ['between', [1, 2]];
            if(isset($recycle_name) && $recycle_name != ''){
                $where['recycle_name'] = ['like', '%' . $recycle_name . '%'];
            }
            if(isset($recycle_status) && $recycle_status != ''){
                $where['recycle_status'] = $recycle_status;
            }
            if(isset($recycle_brand_id) && $recycle_brand_id != ''){
                $where['recycle_brand_id'] = $recycle_brand_id;
            }
            if(isset($recycle_models_id) && $recycle_models_id != ''){
                $where['recycle_models_id'] = $recycle_models_id;
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('recycle')
                ->join('up_brand', 'brand_id = recycle_brand_id', 'LEFT')
                ->join('up_models', 'models_id = recycle_models_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('recycle')
                ->join('up_brand', 'brand_id = recycle_brand_id', 'LEFT')
                ->join('up_models', 'models_id = recycle_models_id', 'LEFT')
                ->field('recycle_id, recycle_name, recycle_sort, recycle_is_more, recycle_status, 
                brand_name, models_name')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('recycle_sort desc, recycle_status desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $brand = Db::table('up_brand')->where('brand_status', 1)->field('brand_id, brand_name')->select();
        $this->assign('brand', $brand);
        return $this->fetch();
    }
    
    /**
     * 添加回收故障
     * @return mixed|\think\response\Json
     */
    public function recycleAdd(){
        if(Request::instance()->isPost()){
            $recycle_brand_id = input('recycle_brand_id');
            $recycle_models_id = input('recycle_models_id');
            $recycle_name = trim(input('recycle_name'));
            $recycle_sort = input('recycle_sort');
            $recycle_is_more = input('recycle_is_more');
            $recycle_status = input('recycle_status');
            
            $insert = [
                'recycle_brand_id' => $recycle_brand_id,
                'recycle_models_id' => $recycle_models_id,
                'recycle_name' => $recycle_name,
                'recycle_sort' => $recycle_sort,
                'recycle_is_more' => $recycle_is_more,
                'recycle_status' => $recycle_status,
                'recycle_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_recycle')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $brand = Db::table('up_brand')->where('brand_status', 1)->field('brand_id, brand_name')->select();
        $this->assign('brand', $brand);
        return $this->fetch();
    }
    
    /**
     * 编辑回收故障
     * @return mixed|\think\response\Json
     */
    public function recycleEdit(){
        $recycle_id = input('recycle_id');
        if(Request::instance()->isPost()){
            $recycle_brand_id = input('recycle_brand_id');
            $recycle_models_id = input('recycle_models_id');
            $recycle_name = trim(input('recycle_name'));
            $recycle_sort = input('recycle_sort');
            $recycle_is_more = input('recycle_is_more');
            $recycle_status = input('recycle_status');
    
            $update = [
                'recycle_id' => $recycle_id,
                'recycle_brand_id' => $recycle_brand_id,
                'recycle_models_id' => $recycle_models_id,
                'recycle_name' => $recycle_name,
                'recycle_sort' => $recycle_sort,
                'recycle_is_more' => $recycle_is_more,
                'recycle_status' => $recycle_status,
                'recycle_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_recycle')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $recycle = Db::table('up_recycle')->where('recycle_id', $recycle_id)
            ->field('recycle_brand_id, recycle_models_id, recycle_name, recycle_sort, recycle_is_more, 
            recycle_status')
            ->find();
        $brand = Db::table('up_brand')->where('brand_status', 1)->field('brand_id, brand_name')->select();
        $models = Db::table('up_models')->where('models_brand_id', $recycle['recycle_brand_id'])->field('models_id, models_name')->select();
        $this->assign([
            'recycle' => $recycle,
            'brand' => $brand,
            'models' => $models,
            'recycle_id' => $recycle_id
        ]);
        return $this->fetch();
    }
    
    /**
     * 下架故障
     * @return \think\response\Json
     */
    public function recycleDown(){
        $recycle_id = input('id');
        $res = Db::table('up_recycle')->where('recycle_id', $recycle_id)->setField('recycle_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 上架故障
     * @return \think\response\Json
     */
    public function recycleUp(){
        $recycle_id = input('id');
        $res = Db::table('up_recycle')->where('recycle_id', $recycle_id)->setField('recycle_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除故障
     * @return \think\response\Json
     */
    public function recycleDel(){
        $recycle_id = input('id');
        $res = Db::table('up_recycle')->where('recycle_id', $recycle_id)->setField('recycle_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 复制回收故障
     * @return mixed|\think\response\Json
     */
    public function copyRecycle(){
        $recycle_ids = input('recycle_id');
        if(Request::instance()->isPost()){
            $brand_id = input('brand_id');
            $models_id = input('models_id');
            
            if(!$recycle_ids) return json(['code' => 100, 'msg' => '请选择需要复制的回收故障']);
            
            $recycle = Db::table('up_recycle')
                ->where('recycle_id', 'IN', $recycle_ids)
                ->field('recycle_id, recycle_name, recycle_sort, recycle_is_more')
                ->select();
            Db::startTrans();
            try{
                foreach($recycle as $k => $v){
                    $insert_recycle = [
                        'recycle_brand_id' => $brand_id,
                        'recycle_models_id' => $models_id,
                        'recycle_name' => $v['recycle_name'],
                        'recycle_sort' => $v['recycle_sort'],
                        'recycle_is_more' => $v['recycle_is_more'],
                        'recycle_status' => 2,
                        'recycle_time' => date('Y-m-d H:i:s')
                    ];
                    $new_recycle_id = Db::table('up_recycle')->insertGetId($insert_recycle);
                    // 原小项
                    $info = Db::table('up_recycle_info')
                        ->where('ri_recycle_id', $v['recycle_id'])
                        ->where('ri_status', 1)
                        ->field('ri_name, ri_price, ri_is_hb, ri_images, ri_word')
                        ->select();
                    if($info){
                        $insert_info_all = [];
                        foreach($info as $ki => $vi){
                            $insert_info_all[$ki] = [
                                'ri_brand_id' => $brand_id,
                                'ri_models_id' => $models_id,
                                'ri_recycle_id' => $new_recycle_id,
                                'ri_name' => $vi['ri_name'],
                                'ri_price' => $vi['ri_price'],
                                'ri_is_hb' => $vi['ri_is_hb'],
                                'ri_images' => $vi['ri_images'],
                                'ri_word' => $vi['ri_word'],
                                'ri_time' => date('Y-m-d H:i:s')
                            ];
                        }
                        Db::table('up_recycle_info')->insertAll($insert_info_all);
                    }
                }
                
                Db::commit();
                return json(['code' => 200, 'msg' => '复制成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => $e->getMessage()]);
            }
            
        }
        $brand = Db::table('up_brand')->where('brand_status', 1)->field('brand_id, brand_name')->select();
        $this->assign('brand', $brand);
        $this->assign('recycle_id', $recycle_ids);
        return $this->fetch();
    }
    
    /**
     * 故障排序
     * @return \think\response\Json
     */
    public function updateSort(){
        $recycle_id = input('id');
        $field = input('field');
        $value = input('value');
        $res = Db::table('up_recycle')->where('recycle_id', $recycle_id)->setField($field, $value);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}